using System.IO;

namespace RawRestructure.Models;

/// <summary>
/// RAW图像文件信息
/// </summary>
public class RawImageInfo
{
    /// <summary>
    /// 图像类型常量
    /// </summary>
    public static class ImageTypes
    {
        /// <summary>
        /// 正位
        /// </summary>
        public const int PA = 1;
        
        /// <summary>
        /// 侧位
        /// </summary>
        public const int LAT = 2;
    }

    /// <summary>
    /// 图像类型：1=正位, 2=侧位
    /// </summary>
    public int ImageType { get; set; }

    /// <summary>
    /// 图像序号
    /// </summary>
    public int ImageNumber { get; set; }

    /// <summary>
    /// 文件名
    /// </summary>
    public string FileName { get; set; } = string.Empty;

    /// <summary>
    /// 完整路径
    /// </summary>
    public string FullPath { get; set; } = string.Empty;

    /// <summary>
    /// 是否为正位图像
    /// </summary>
    public bool IsPA => ImageType == ImageTypes.PA;

    /// <summary>
    /// 是否为侧位图像
    /// </summary>
    public bool IsLAT => ImageType == ImageTypes.LAT;

    /// <summary>
    /// 图像类型显示名称
    /// </summary>
    public string ImageTypeDisplayName => ImageType switch
    {
        ImageTypes.PA => "正位(PA)",
        ImageTypes.LAT => "侧位(LAT)",
        _ => "未知"
    };

    /// <summary>
    /// 创建RawImageInfo实例
    /// </summary>
    /// <param name="fileName">文件名</param>
    /// <param name="fullPath">完整路径</param>
    /// <returns>RawImageInfo实例，如果文件名格式不正确则返回null</returns>
    public static RawImageInfo? CreateFromFileName(string fileName, string fullPath)
    {
        // 解析文件名格式：A_B.raw
        var fileNameWithoutExt = Path.GetFileNameWithoutExtension(fileName);
        var parts = fileNameWithoutExt.Split('_');
        
        if (parts.Length != 2)
            return null;

        if (!int.TryParse(parts[0], out int imageType) || 
            !int.TryParse(parts[1], out int imageNumber))
            return null;

        // 验证图像类型必须是1或2
        if (imageType != ImageTypes.PA && imageType != ImageTypes.LAT)
            return null;

        // 验证图像序号范围（1-9999）
        if (imageNumber < 1 || imageNumber > 9999)
            return null;

        return new RawImageInfo
        {
            ImageType = imageType,
            ImageNumber = imageNumber,
            FileName = fileName,
            FullPath = fullPath
        };
    }
} 